package com.wenwei.sharebox.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.wenwei.sharebox.R;
import com.wenwei.sharebox.base.BaseActivity;
import com.wenwei.sharebox.base.Constant;
import com.wenwei.sharebox.bean.RefundBean;
import com.wenwei.sharebox.bean.RentBean;
import com.wenwei.sharebox.bean.UserShareBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;
import com.wenwei.sharebox.view.SecureInputView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/11.
 */
public class InputCodeActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.input)
    SecureInputView input;
    @Bind(R.id.input_ok)
    TextView inputOk;

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_input_code;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("手动输入");
    }

    @OnClick({R.id.base_back, R.id.input_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.input_ok:
                if(input.getText().toString().length() ==6){
//                    Bundle bundle = new Bundle();
//                    bundle.putString("deviceCode",input.getText().toString());
//                    goActivity(mContext,ShareConfirmActivity.class,bundle);

                    goToActivity(input.getText().toString());
                }
                break;
        }
    }


    private int mQrType;
    private void goToActivity(String deviceCode){
        mQrType = getIntent().getIntExtra("qr_type",0);
        Bundle bundle = new Bundle();
        bundle.putString("deviceCode",deviceCode);
        switch (mQrType){
            case Constant.QR_TYPE_CHECK:
                goActivity(mContext,ProvideCheckActivity.class,bundle);
                break;
            case Constant.QR_TYPE_REPLENISHMENT:
                goActivity(mContext,ReplenishmentActivity.class,bundle);
                break;
            case Constant.QR_TYPE_REPAIR:
                goActivity(mContext,RepairActivity.class,bundle);
                break;
            case Constant.QR_TYPE_GOODS:
                goActivity(mContext,ShareConfirmActivity.class,bundle);
                break;
            case Constant.QR_TYPE_RETURN:
                //申请退款
                returnMoney(deviceCode);
                break;
            case Constant.QR_TYPE_REFUND:
                //申请归还
                refundGoods(deviceCode);
                break;
        }
    }


    //申请退款
    private int orderId;
    private String reasonStr;
    BaseSubscriber<RefundBean> refundSubscriber;
    private void returnMoney(String deviceNum){
        orderId = getIntent().getIntExtra("orderId",0) ;
        reasonStr = getIntent().getStringExtra("reason");
        refundSubscriber = new BaseSubscriber<RefundBean>(mContext,true,"请稍后...") {
            @Override
            public void onSuccess(RefundBean refundBean) {
                goActivity(mContext,OperationSuccessActivity.class);
            }
        };
        ApiManager.getInstance().refund(refundSubscriber,orderId,deviceNum,reasonStr);
    }

    //申请归还


    private BaseSubscriber<RentBean> rentSubscriber;
    private void refundGoods(String deviceNum){
        orderId = getIntent().getIntExtra("orderId",0) ;
        rentSubscriber = new BaseSubscriber<RentBean>(mContext) {
            @Override
            public void onSuccess(RentBean rentBean) {
                 goActivity(mContext,OperationSuccessActivity.class);
            }
        };
        ApiManager.getInstance().rent(rentSubscriber,orderId,deviceNum);
    }
}
